import {Restivus} from 'meteor/nimble:restivus';
import {Meteor} from 'meteor/meteor';
import { Random } from 'meteor/random';
// const mqttUrl = '119.254.97.31';
// const mqttUrl = '192.168.89.35';
const mqttUrl = 'nodeapis.miitzc.com';
import {lockProjects} from '../imports/api/lock_projects';

if (Meteor.isServer) {
    Meteor.startup(() => {
        let Api = new Restivus({
            useDefaultAuth: true,
            prettyJson: true,
        });
        // 添加锁密码
        Api.addRoute('addLockPwd', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const deviceId = param.deviceId;
                        const productNumber = param.productNumber;
                        const gatewayId = param.gatewayId;
                        const pwd = param.password;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                                try {
                                    const result = HTTP.call('POST', `http://${mqttUrl}/addPwd`, {
                                        data: { productNumber: productNumber, devID: deviceId, gatewayId: gatewayId, pwd: pwd}
                                    });
                                    const content = JSON.parse(result.content);
                                    if (content.success === true) {
                                        return  {success: true, result: '密码添加成功!', resultCode: 1000};
                                    }else{
                                        return  {success: false, result: '密码添加失败！', resultCode: 1004};
                                    }
                                } catch (e) {
                                    return  {success: false, result: '密码添加失败！', resultCode: 1004};
                                }

                        }else{
                            return {success: false, result: '请登录！', resultCode: 1004};
                        }
                    }
                    return {success: false, result: '没有获取到登录用户的信息！', resultCode: 1002};
                }
            }
        });
        // 添加一次性锁密码
        Api.addRoute('addOneLockPwd', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const deviceId = param.deviceId;
                        const productNumber = param.productNumber;
                        const gatewayId = param.gatewayId;
                        const pwd = param.password;
                        const times = param.times;
                        const startTime = param.startTime;
                        const endTime = param.endTime;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        if(user) {
                            try {
                                const result = HTTP.call('POST', `https://smart.miitzc.com/tmall/appSetOncePwd`, {
                                    data: { productNumber: productNumber, devID: deviceId, gatewayId: gatewayId, pwd: pwd, times: times, startTime: startTime, endTime: endTime}
                                });
                                const content = JSON.parse(result.content);
                                if (content.success === true) {
                                    return  {success: true, result: '一次性密码添加成功!', resultCode: 1000};
                                }else{
                                    return  {success: false, result: '一次性密码添加失败！', resultCode: 1004};
                                }
                            } catch (e) {
                                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                                return  {success: false, result: '一次性密码添加失败！', resultCode: 1004};
                            }
                        }else{
                            return {success: false, result: '请登录！', resultCode: 1004};
                        }
                    }
                    return {success: false, result: '没有获取到登录用户的信息！', resultCode: 1002};
                }
            }
        });
        //锁密码列表
        Api.addRoute('getPwdList', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const deviceId = param.deviceId;
                        // const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        // if(user) {
                            const lps = lockProjects.find({deviceId: deviceId}).fetch();
                            if(lps.length > 0){
                                return {success: true, result: lps, resultCode: 1000}
                            }else{
                                return {success: true, result: [], resultCode: 1000}
                            }
                        // }else{
                        //     return {success: false, result: '请登录！', resultCode: 1004};
                        // }
                    }
                    // return {success: false, result: '没有获取到登录用户的信息！', resultCode: 1002};
                }
            }
        });
    });

}